<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户单号" prop="merchOrder">
        <el-input class="myMiniInput" v-model="queryParams.merchOrder" placeholder="请输入商户单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付通道" prop="payWay">
         
          <el-select v-model="queryParams.payWay"  clearable  placeholder="请输入支付通道"  @change="handleQuery" >
                <el-option label="快手支付宝原生" value="KS_ALI_Y" ></el-option>
                <el-option label="快手微信原生" value="KS_WX_Y" ></el-option>
                <el-option label="快手二维码支付" value="KS_EWM_A" ></el-option>
              </el-select>

      </el-form-item>


      <el-form-item label="商户名" prop="merchName">
        <el-input class="myMiniInput" v-model="queryParams.merchName" placeholder="请输入商户名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="支付状态" prop="orderStage">
        <el-select v-model="queryParams.orderStage"  clearable  placeholder="请输入订单状态"  @change="handleQuery" >
                <el-option
                v-for="dict in dict.type.sys_order_stage"
                :key="dict.value"
                :label="dict.label   "
                  :value="dict.value  " 
                ></el-option>
              </el-select>

        
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 
    <el-table v-loading="loading" :data="sysorderList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="商户名" align="center" prop="merchName" />
      <el-table-column label="支付通道" align="center" width="180px" prop="payWay" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-select v-model="scope.row.payWay" class="tableSelect" placeholder="请选择所属平台">
            <el-option v-for="item in syspaywayList" :key="item.wayCode" :label="item.wayName"
              :value="item.wayCode"></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="商户单号" align="center" width="140px" prop="merchOrder" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单金额" align="center" prop="orderMoney" />
      <!-- <el-table-column label="支付金额" align="center" prop="payMoney" /> -->
      <el-table-column label="状态" align="center" prop="orderStage" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_order_stage" :value="scope.row.orderStage"/>
        </template>
        </el-table-column>
      <el-table-column label="用户ip" align="center" prop="userIp" />
      <el-table-column label="设备" align="center" prop="userEqu" />
      <el-table-column label="回调" align="center" prop="callbackStage" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="(scope.row.orderStage==3)" type="success" icon="el-icon-refresh-right" circle @click="callback(scope.row)" size="small"
          v-hasPermi="['system:ksOrder:callBcke']"
          ></el-button>  
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body label-position="left">

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="商户名" prop="merchName" class="tableSelect2">
              <el-input v-model="form.merchName" placeholder="请输入商户名" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商户单号" prop="merchOrder" class="tableSelect2">
              <el-input v-model="form.merchOrder" placeholder="请输入商户单号" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="支付通道" prop="payWay">
              <el-select v-model="form.payWay" class="tableSelect2" disabled placeholder="请选择所属平台">
                <el-option v-for="item in syspaywayList" :key="item.wayCode" :label="item.wayName"
                  :value="item.wayCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单状态" prop="orderStage" class="tableSelect2"> 
              <el-select v-model="form.orderStage"  class="tableSelect2"  placeholder="请输入订单状态" disabled>
                <el-option
                v-for="dict in dict.type.sys_order_stage"
                :key="dict.value"
                :label="dict.label   "
                  :value="dict.value  " 
                ></el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="下单金额" prop="orderMoney" class="tableSelect2">
              <el-input v-model="form.orderMoney" placeholder="请输入下单金额" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付金额" prop="payMoney" class="tableSelect2">
              <el-input v-model="form.payMoney" placeholder="请输入支付金额" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="支付时间" prop="payTime" class="tableSelect">
              <el-date-picker clearable v-model="form.payTime" type="date" disabled value-format="yyyy-MM-dd"
                placeholder="请选择支付时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="payTime" class="tableSelect">
              <el-date-picker clearable v-model="form.createTime" disabled type="date" value-format="yyyy-MM-dd"
                placeholder="请选择创建时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="用户ip" prop="userIp" class="tableSelect2">
              <el-input v-model="form.userIp" placeholder="请输入用户ip" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备" prop="userEqu" class="tableSelect2">
              <el-input v-model="form.userIp" placeholder="设备" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="12">
            <el-form-item label="回调状态" prop="callbackStage" class="tableSelect2">
              <el-input v-model="form.callbackStage" placeholder="请输入回调状态" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark" class="tableSelect2">
              <el-input v-model="form.remark" placeholder="" disabled />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysorder, getSysorder, delSysorder, sendCallBack, addSysorder, updateSysorder } from "@/api/system/sysorder";
import { listSyspayway } from "@/api/system/syspayway";
import { listKsOrder } from "@/api/ks/ksorder";

export default {
  name: "Sysorder",
  dicts: ['sys_order_stage'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付方式列表
      syspaywayList: [],
      // 订单管理表格数据
      sysorderList: [],
      // 弹出层标题
      title: "",
       // 日期范围
       dateRange: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payWays: ['KS_ALI_Y','KS_WX_Y','KS_EWM_A'], 
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchName: [
          { required: true, message: "商户名不能为空", trigger: "blur" }
        ],
        merchWay: [
          { required: true, message: "商户通道不能为空", trigger: "blur" }
        ],
        orderMoney: [
          { required: true, message: "下单金额不能为空", trigger: "blur" }
        ],
        callbackUrl: [
          { required: true, message: "回调路径不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getPayWayList();
  },
  methods: {
    /** 查询支付通道列表 */
    getPayWayList() {
      listSyspayway({ pageNum: 1, pageSize: 100 }).then(response => {
        this.syspaywayList = response.rows;

      });
    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listKsOrder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.sysorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        merchName: null,
        merchWay: null,
        payWay: null,
        merchOrder: null,
        payTime: null,
        orderMoney: null,
        payMoney: null,
        orderStage: null,
        userIp: null,
        userEqu: null,
        callbackStage: null,
        callbackUrl: null,
        callbackCount: null,
        deptId: null,
        logicDelect: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getSysorder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateSysorder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 主动回调按钮操作 */
    callback(row) { 
      const that = this;
      this.$modal.confirm('是否确认发送回调请求？').then(function () {
         sendCallBack(row).then(response => { 
          that.getList();
          console.log(response)
          if(response.msg==="请求失败"){
            that.$modal.msgError("回调失败");
          }else{
            that.$modal.msgSuccess("回调成功");
            
          }
          
        });
        return
      }).then(response => { 
           
      }).catch(() => { });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + orderIds + '"的数据项？').then(function () {
        return delSysorder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sysorder/export', {
        ...this.queryParams
      }, `sysorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.myMiniInput {
  width: 138px;
}
</style>